// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require(CONFIG.BasePath + "js/enterprise/base");
    var router = require("../hash/pageRouter");
    var constant = require("constant");
    var css_detail = require("./accountDetailMaterial.css");
	var html_cancelApplication = require("./html_cancelApplication.html");
	var html_verify = require("../apply/apply_verify.html");
	var html_bailReturn = require("./html_bailReturn.html");
    var databind = require("databind");
    var tools = require("tools");
    var financeId;
    var type;
    var productType;
	var enterpriseId;
	var financeNumber;
	var enterpriseName;
    var fileName;
    var financeState;
    
    var init = function(page){
    	financeId = page.params.financeId;
    	type = page.params.type;
    	inquireFinanceApplicationDetail();
    	$(".d2 a").removeClass("active_tab");
    	$(".accountMaterial").addClass("active_tab");
    	$(".accountDetail").parent().parent().addClass("active_tab3");
    	$(".accountDetail").parent().parent().removeClass("active_tab1");
    	$(".accountDetail").parent().parent().removeClass("active_tab2");
    	$(".accountDetail").parent().parent().removeClass("active_tab4");
    	router.init();
		initEvent();
    };
    
	var initEvent = function(){
		if(base.getData("sid")==null){
			location.href="../login/login.html";
		}
		
		$(".accountDetail").unbind("click").click(function(){
			router.route("account/accountDetail.html?financeId="+financeId + "&type="+type);
		});
		
		$(".accountProcess").unbind("click").click(function(){
			router.route("account/accountDetailProcess.html?financeId=" + financeId + "&type="+type+ "&enterpriseId=" +enterpriseId +"&enterpriseName="+enterpriseName+"&productType="+productType);
		});
		//提货列表
		$(".warehousePickups").unbind("click").click(function() {
			router.route("account/warehousePickupList2.html?financeId=" + financeId + "&type="+type);
		});
    };
    
  //提交融资申请
	var submitFinanceApplication = function() {
		base.fnAjax({
			action: "submitFinanceApplication.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					router.route("account/accountMain.html");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};
	
	//取消融资申请
	var cancelFinanceApplication = function() {
		base.fnAjax({
			action: "cancelFinanceApplication.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "操作成功！");
					dialog.hideDialog($("#applyVerify"));
					router.route("account/accountMain.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	//查询评价信息
	var inquireFinanceEvaluate = function() {
		base.fnAjax({
			action: "inquireFinanceEvaluate.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas[0].content == "") {
						$(".buttonOperate").html("评价").attr("id", "evalute");
					} else {
						$(".buttonOperate").remove();
					}
				} else {
					$(".buttonOperate").html("评价").attr("id", "evalute");
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};
	//货物一览
	var bulkstockListing = function() {
		router.route("account/accountBulkstockListing.html?financeId=" + financeId + "&productType=" + productType);
	}
	//融资评价
	var evaluteFinanceApplication = function() {
		router.route("account/accountEvaluate.html?financeId=" + financeId);
	}
	//合同一览
	var documentSign = function() {
		router.route("account/documentSign.html?financeId=" + financeId+"&financeState="+financeState);
	}
	var showEvaluteDetail = function(){
		router.route("account/accountEvaluateDetail.html?financeId="+financeId);
	}
	//额度处理
	var verifyFinanceApplication = function() {
		$("#popdialog").html(html_verify);
		dialog.showDialog($("#applyVerify"));
		$(".financeNumber").html(financeNumber);
		$(".financeAmount").html("￥" + financeAmount);

		databind.fillInput($("#applyVerify"), {
			action: "inquireCurrentEnterpriseDetail.json",
			version: "v1.0"
		});
		$("#verifySure").unbind("click").click(function() {
			consentFinanceAuditAmountApplication();
		});

		$("#verifyCancel").unbind("click").click(function() {
			cancelFinanceApplication();
		});
	}

	//接受融资审批金额
	var consentFinanceAuditAmountApplication = function() {
		base.fnAjax({
			action: "consentFinanceAuditAmountApplication.json",
			async: false,
			oData: {
				financeId: financeId
			},
			fnSuccess: function(data) {
				tools.cancelProtectDoubleClick();
				if (data.status == 200) {
					base.alert(1, "操作成功！");
					dialog.hideDialog($("#applyVerify"));
					router.route("account/accountMain.html");
				} else {
					base.alert(2, "数据获取失败");
				}
			}
		});
	};
	//提货申请
	var pickupFinanceApplication = function() {
		router.route("account/accountPickup.html?financeId=" + financeId + "&financeNumber=" + financeNumber);
	}
	//修改融资申请
	var updateFinanceApplication = function() {
		location.href = "../apply/apply_main.html?financeId=" + financeId + "&productType=" + productType;
	}
    

	//企业查询融资信息详情
    var inquireFinanceApplicationDetail = function(){
		base.fnAjax({
			action: "inquireFinanceApplicationDetail.json",
			async: false,
			oData: {
                financeId: financeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	var tpl = $("#accountMaterialTpl").html();
                	var html = juicer(tpl, data);
                	$("#accountMaterialBody").html(html);
                	productType = data.datas[0].productType;
                	enterpriseId = data.datas[0].enterpriseId;
                	financeNumber = data.datas[0].financeNumber;
                	enterpriseName = data.datas[0].enterpriseName;
                	financeState = data.datas[0].financeState;
                	var materialItems = $(".materialItem .d11");
                	for(var j = 0; j<materialItems.length; j++){
                		$(materialItems[j]).find(" span").html(tools.inquireFile($(materialItems[j]).attr("materialFileuuid")));
                		for (var k=materialItems.length-j+1;k>1;k--){
                			if($(materialItems[k]).attr("templateId") == $(materialItems[k-1]).attr("templateId")){
                				$(materialItems[k]).find(".d1").clone().appendTo($(materialItems[k-1]));
                				$(materialItems[k]).remove();
                				k = 1;
                			}
                		}
                	}
                	$.each($(".materialItem"), function() {
                		var _this = $(this);
                		
                		if($.trim(_this.find(".d1").html())==""){
                			_this.remove();
                		};
                	});
                	if(productType == 2){
						if (financeState == 1) {
							
						} else if (financeState == 2) {
							$(".buttonOperate3").html("额度处理").attr("id", "verifyApply2").removeClass("none");
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="cancelApply2">取消申请</a>');
						} else if (financeState == 4) {
							$(".buttonOperate3").html("修改").attr("id", "modifyApply2").removeClass("none");
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="cancelApply2">取消申请</a>');
						} else if (financeState == 17) {
							inquireFinanceEvaluate();
						} else if (financeState == 18) {
							$(".buttonOperate3").html("评价").attr("id", "evalute2").removeClass("none");
						} else if (financeState == 19) {
							$(".buttonOperate3").html("评价信息").attr("id", "evaluteDetail2").removeClass("none");
						} else if (financeState == 20) {
							$(".buttonOperate3").html("取消申请").attr("id", "cancelApply2").removeClass("none");
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="modifyApply2">修改</a>');
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="submitApply2">提交融资</a>');
						} else {
							$(".buttonOperate3").hide();
						}
						
						if (data.datas[0].bail - data.datas[0].bailAlreadyReturn > 0) {
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="bailReturn2">退还保证金</a>');
						}
						if (financeState > 5) {
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="bulkstockListing2">货物一览</a>');
						}
						if (financeState == 24 || financeState == 23 || financeState == 22) {
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="documentSign2">合同签署</a>');
						}
	
						if (financeState >= 6) {
							$(".exceptionOperate").removeClass("none");
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="pickupApply2">申请提货</a>');
						}
					}else if(productType == 6){
						if (data.datas[0].bail - data.datas[0].bailAlreadyReturn > 0) {
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="bailReturn2">退还保证金</a>');
						}
						if (financeState > 5) {
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="bulkstockListing2">货物一览</a>');
						}
						if (financeState == 24 || financeState == 23 || financeState == 22) {
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="documentSign2">合同签署</a>');
						}
	
						$(".buttonOperate3:first").before('<a class="buttonOperate3" id="createPickup11s2">提货</a>');
					}else if(productType == 5){
						if (financeState > 5) {
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="bulkstockListing2">货物一览</a>');
						}
						if (financeState == 24 || financeState == 23 || financeState == 22) {
							$(".buttonOperate3:first").before('<a class="buttonOperate3" id="documentSign2">合同签署</a>');
						}
	
						$(".buttonOperate3:first").before('<a class="buttonOperate3" id="createPickup11s2">提货</a>');
					}
					
					$(".buttonOperate3:first").before('<a class="buttonOperate3" id="calcelApply2">融资申请取消</a>');
					
					databind.disableInput(".content2");
					$("#calcelApply2").unbind("click").click(function(){
						var ii = layer.open({
						  title: '融资取消申请',
						  type: 1,
						  skin: 'layui-layer-rim', //加上边框
						  area: ['800px', '300px'], //宽高
						  content: html_cancelApplication,
						  btn: ['确定', '取消'],
						  yes: function(){
						  	databind.submitDom($(".createFinanceCancelApplication"), function(){
						  		layer.msg("申请成功！");
						  		layer.close(ii);
						  		router.route("account/accountDetail.html?financeId=" + financeId);
						  	});
						  }
						});
						
						$(".createFinanceCancelApplication input[name=financeId]").val(financeId);
					});
					//取消融资
					$("#cancelApply2").unbind("click").click(function() {
						dialog.showConfirmDialog("取消融资", "是否确定取消融资？", function(){
							cancelFinanceApplication();
						});
					});
					//提交融资
					$("#submitApply2").unbind("click").click(function() {
						dialog.showConfirmDialog("提交融资", "提交以后不能再次修改,是否确定提交融资？", function(){
							submitFinanceApplication();
						});
					});
					
					$("#verifyApply2").unbind("click").click(function() {
						verifyFinanceApplication();
					});
					
					//修改融资
					$("#modifyApply2").unbind("click").click(function() {
						updateFinanceApplication();
					});
					//评价
					$("#evalute2").unbind("click").click(function() {
						evaluteFinanceApplication();
					});
					//货物一览
					$("#bulkstockListing2").unbind("click").click(function() {
						bulkstockListing();
					});
					//合同一览
					$("#documentSign2").unbind("click").click(function() {
						documentSign();
					});
					//保证金退还
					$("#bailReturn2").unbind("click").click(function() {
						var ii = layer.open({
						  title: '保证金退还',
						  type: 1,
						  skin: 'layui-layer-rim', //加上边框
						  area: ['300px', '150px'], //宽高
						  content: html_bailReturn,
						  btn: ['确定', '取消'],
						  yes: function(){
						  	databind.submitDom($(".createBailReturn"), function(){
						  		layer.msg("申请成功！");
						  		layer.close(ii);
						  	});
						  }
						});
						$(".createBailReturn input[name=financeId]").val(financeId);
						tools.clearNoNumAll($(".createBailReturn"));
					});
					//提货
					$("#createPickup11s2").unbind("click").click(function(){
						if(productType == 5){
							router.route("account/createPickup6.html?enterpriseId=" + enterpriseId +"&financeNumber=" + financeNumber+"&enterpriseName="+enterpriseName+"&productType="+productType+"&financeId="+financeId);
						}else if(productType == 6){
							router.route("account/createPickup4.html?enterpriseId=" + enterpriseId +"&financeNumber=" + financeNumber+"&enterpriseName="+enterpriseName+"&productType="+productType+"&financeId="+financeId);
						}
						
					});
					//查看评价信息
					$("#evaluteDetail2").unbind("click").click(function() {
						showEvaluteDetail();
					});
					//提货申请
					$("#pickupApply2").unbind("click").click(function() {
						pickupFinanceApplication();
					});
                } else {
//                  base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
    return {
        init:init
    };
});